// webpack配置文件
// entry 打包入口
//output 出口文件

// 配置js
// css less scss
// png jpg
// json
// vue/react

// 起服务
const path = require('path')
const htmlWebpackPlugin = require('html-webpack-plugin')
const openBrowserWebpackPlugin = require('open-browser-webpack-plugin')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const webpack = require('webpack')
module.exports = {
    entry: ['./src/main.js'],//多个入口
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'js/app.[hash:8].js',//通过哈希算法得到长度八位的字符串
        publicPath: '',//打包的公共路径
    },
    module: {
        rules: [
            {
                test: /\.(js|jsx|ts|tsx)$/,
                exclude: /node_modules/,
                use: ["babel-loader"]
            },
            {
                test: /\.(jpg|gif|png|svg|gif|woff|woff2|ttf|eot)$/,
                exclude: /node_modules/,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            limit: 8192,//8M
                            name: 'img/[name].[hash:8].[ext]',
                            fallback: 'file-loader',//当超过8192时，会回退使用fallback
                            publicPath: ''//采用根路径
                        },
                        // query: ''
                    }
                ]
            },
            {
                test: /\.(css|scss|sass)$/,
                use: ExtractTextPlugin.extract({
                    fallback: 'style-loader',//把样式代码转化为字符串的js代码块
                    use: [
                        'css-loader',//编译为commonJS模块
                        {
                            loader: 'postcss-loader',
                            options: {
                                plugins: function () {
                                    require('cssgrace'),//代码美化缩进
                                        require('autoprefixer'),//自动补全webkit ms moz
                                        require('postcss-px2rem')({

                                        })
                                }
                            }
                        }
                    ]
                })
            }

        ]
    },

    resolve: {
        alias: {
            "@": path.resolve('src'),
            "~": path.resolve('src/utils')
        }
    },
    devtool: "source-map",
    devServer: {    //webpack服务器配置
        contentBase: path.resolve(__dirname, 'dist'),//服务器的启动目录
        host: 'localhost',
        port: 2000,
        inline: true,
        hot: true,
        compress: true,
        // proxy: {

        // },
        // open: true
    },
    plugins: [
        new htmlWebpackPlugin({
            template: './public/index.html',
            inject: true,//自动引入打包 的css/js
            minify: true
        }),
        new openBrowserWebpackPlugin({
            url: 'http://localhost:2000'
        }),
        new ExtractTextPlugin({
            filename: 'css/app.[hash:8].css',
            allChunks: true, //抽离所有样式
            disable: false, //不抽离？
        }),
        new webpack.ProvidePlugin({
            React: 'React',
            Component: ['React', 'Component']
        })
    ]
}